package org.jeecg.modules.demo.ch.service.impl;

import org.jeecg.modules.demo.ch.entity.ChSoftwareLicensingManagement;
import org.jeecg.modules.demo.ch.entity.ChSoftwareUseDetails;
import org.jeecg.modules.demo.ch.mapper.ChSoftwareUseDetailsMapper;
import org.jeecg.modules.demo.ch.mapper.ChSoftwareLicensingManagementMapper;
import org.jeecg.modules.demo.ch.service.IChSoftwareLicensingManagementService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 软件授权使用管理
 * @Author: jeecg-boot
 * @Date:   2022-12-16
 * @Version: V1.0
 */
@Service
public class ChSoftwareLicensingManagementServiceImpl extends ServiceImpl<ChSoftwareLicensingManagementMapper, ChSoftwareLicensingManagement> implements IChSoftwareLicensingManagementService {

	@Autowired
	private ChSoftwareLicensingManagementMapper chSoftwareLicensingManagementMapper;
	@Autowired
	private ChSoftwareUseDetailsMapper chSoftwareUseDetailsMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(ChSoftwareLicensingManagement chSoftwareLicensingManagement, List<ChSoftwareUseDetails> chSoftwareUseDetailsList) {
		chSoftwareLicensingManagementMapper.insert(chSoftwareLicensingManagement);
		if(chSoftwareUseDetailsList!=null && chSoftwareUseDetailsList.size()>0) {
			for(ChSoftwareUseDetails entity:chSoftwareUseDetailsList) {
				//外键设置
				entity.setLicensingManagementId(chSoftwareLicensingManagement.getId());
				chSoftwareUseDetailsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(ChSoftwareLicensingManagement chSoftwareLicensingManagement,List<ChSoftwareUseDetails> chSoftwareUseDetailsList) {
		chSoftwareLicensingManagementMapper.updateById(chSoftwareLicensingManagement);
		
		//1.先删除子表数据
		chSoftwareUseDetailsMapper.deleteByMainId(chSoftwareLicensingManagement.getId());
		
		//2.子表数据重新插入
		if(chSoftwareUseDetailsList!=null && chSoftwareUseDetailsList.size()>0) {
			for(ChSoftwareUseDetails entity:chSoftwareUseDetailsList) {
				//外键设置
				entity.setLicensingManagementId(chSoftwareLicensingManagement.getId());
				chSoftwareUseDetailsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		chSoftwareUseDetailsMapper.deleteByMainId(id);
		chSoftwareLicensingManagementMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			chSoftwareUseDetailsMapper.deleteByMainId(id.toString());
			chSoftwareLicensingManagementMapper.deleteById(id);
		}
	}
	
}
